package controller;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.DepartmentService;
import service.DoctorService;
import pojo.Department;
import pojo.Doctor;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;

@RestController
@RequestMapping("/departments")
@Api(tags = "部门管理", description = "部门相关的CRUD操作")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DoctorService doctorService;

    @PostMapping("/add")
    @ApiOperation(value = "新增部门", notes = "添加一个新的部门信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Integer.class),
            @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public int addDepartment(@ApiParam(value = "部门实体", required = true) @RequestBody Department department) {
        return departmentService.insertDepartment(department);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门", notes = "根据部门ID删除部门")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Integer.class),
            @ApiResponse(code = 404, message = "部门不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public int deleteDepartment(@ApiParam(value = "部门ID", required = true) @PathVariable("id") int departmentId) {
        return departmentService.deleteDepartmentById(departmentId);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新部门", notes = "更新部门的基本信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Integer.class),
            @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 404, message = "部门不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public int updateDepartment(@ApiParam(value = "部门实体(包含ID)", required = true) @RequestBody Department department) {
        return departmentService.updateDepartment(department);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取部门详情", notes = "根据部门ID获取部门信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Department.class),
            @ApiResponse(code = 404, message = "部门不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public Department getDepartment(@ApiParam(value = "部门ID", required = true) @PathVariable("id") int departmentId) {
        return departmentService.selectDepartmentById(departmentId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取部门列表", notes = "获取所有部门的基本信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Department.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public List<Department> getAllDepartments() {
        return departmentService.selectAllDepartments();
    }

    // 新增的接口方法，用于获取包含医生信息的部门列表
    @GetMapping("/list-with-doctors")
    @ApiOperation(value = "获取部门列表(含医生信息)", notes = "获取所有部门的信息列表，包含每个部门下的医生信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Department.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public List<Department> getAllDepartmentsWithDoctors() {
        return departmentService.selectAllDepartmentsWithDoctors();
    }

    // 调试接口：检查数据库中的数据
    @GetMapping("/debug/data")
    @ApiOperation(value = "调试数据", notes = "检查数据库中的部门和医生数据")
    public Map<String, Object> debugData() {
        Map<String, Object> result = new HashMap<>();
        
        // 获取所有部门
        List<Department> departments = departmentService.selectAllDepartments();
        result.put("departments", departments);
        result.put("departmentCount", departments.size());
        
        // 获取所有医生
        List<Doctor> doctors = doctorService.selectAllDoctors();
        result.put("doctors", doctors);
        result.put("doctorCount", doctors.size());
        
        // 检查医生与部门的关联
        List<Map<String, Object>> doctorDeptMapping = new ArrayList<>();
        for (Doctor doctor : doctors) {
            Map<String, Object> mapping = new HashMap<>();
            mapping.put("docId", doctor.getDocId());
            mapping.put("docName", doctor.getDocName());
            mapping.put("departId1", doctor.getDepartId1());
            mapping.put("departId2", doctor.getDepartId2());
            doctorDeptMapping.add(mapping);
        }
        result.put("doctorDepartmentMapping", doctorDeptMapping);
        
        return result;
    }
}